package com.handpay.zztong.hp.login.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.HttpEngine;
import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConstant;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.login.bill.AuthCodeBill;
import com.handpay.zztong.hp.ui.VerificationCodeButton;
import com.handpay.zztong.hp.utils.AccountUtils;

import java.util.Hashtable;

import se.krka.kahlua.vm.LuaTable;


/**
 * 账账通开通
 *
 * @author ygchen
 */
public class FindPwdActivity extends ZZTongActivity implements OnClickListener {


    private EditText accountEditText;
    private EditText passwordEditText;
    private EditText confirmPWDEditText;
    private EditText verifyCodeEditText;

    private VerificationCodeButton sendVerifycodeButton;

    private String account;
    private String password;
    private String verifyCode;
    private AuthCodeBill authCodeBill;

    @Override
    protected boolean isShownTabs() {
        return false;
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.findpassword),
                true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setContentView(R.layout.activity_find_pwd);
        super.onCreate(savedInstanceState);
        this.accountEditText = (EditText) findViewById(R.id.accountnumber);
        this.passwordEditText = (EditText) findViewById(R.id.newpassword);
        this.confirmPWDEditText = (EditText) findViewById(R.id.surepassword);
        this.verifyCodeEditText = (EditText) findViewById(R.id.editText_SMS_verifyCode);
        this.sendVerifycodeButton = (VerificationCodeButton) findViewById(R.id.button_send_verifycode);
        this.sendVerifycodeButton.setOnClickListener(this);
        Button confirmButton = (Button) findViewById(R.id.confirmButton);
        confirmButton.setOnClickListener(this);
        authCodeBill = new AuthCodeBill(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        if (i == R.id.button_send_verifycode) {
            account = accountEditText.getText().toString();
            if (!TextUtils.isEmpty(account)) {
                authCodeBill.sendAuthCode(ZZTConstant.AUTHCODE_ACTION_RESETPWD, account);
                sendVerifycodeButton.startLocker(-1);
            } else {
                Toast.makeText(this, R.string.input_account_tip_please, Toast.LENGTH_SHORT).show();
            }

        } else if (i == R.id.confirmButton) {
            account = accountEditText.getText().toString();
            password = passwordEditText.getText().toString();
            String confirmPassword = confirmPWDEditText.getText().toString();
            verifyCode = verifyCodeEditText.getText().toString();
            if (TextUtils.isEmpty(account)) {
                Toast.makeText(this, R.string.input_account_tip_please, Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(verifyCode)) {
                Toast.makeText(this, R.string.no_input_verifycode, Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(password)) {
                Toast.makeText(this, R.string.no_input_password, Toast.LENGTH_SHORT).show();
                return;
            }
            if (!AccountUtils.isPWDLegal(password,7)) {
                Toast.makeText(this, R.string.illegal_input_password, Toast.LENGTH_SHORT).show();
                return;
            }
            if (!password.equals(confirmPassword)) {
                Toast.makeText(this, R.string.input_error_reset_zzt_pwd_tip, Toast.LENGTH_SHORT).show();
                return;
            }
            if (password != null && !"".equals(password)
                    && password.equals(confirmPassword)) {
                doResetPwd();
            }
        }
    }

    private void doResetPwd() {
        Hashtable<String, String> params = new Hashtable<>();
        params.put("account", SecureManager.getInstance().des(account, 1, null));
        params.put("newPwd", SecureManager.getInstance().des(password, 1, null));
        params.put("action", ZZTConstant.AUTHCODE_ACTION_RESETPWD);
        params.put("verifyCode", verifyCode);
        params.put("channel", ClientEngine.CHANNEL);
        doJsonPost(this, Do_ModifyResetPassword, ResponseBean.class, params);

    }

    @Override
    public boolean netJsonResponse(String action, ResponseBean resp, boolean showAlert) {
        boolean result = super.netJsonResponse(action, resp, showAlert);
        if (!result) {
            if (action.equals(Do_SendPhoneCode)) {
                if (resp.getResponseCode().equals("0")) {
                    Toast.makeText(FindPwdActivity.this, "发送验证码成功！", Toast.LENGTH_SHORT).show();
                    //                sendVerifycodeButton.cancelLocker();
                } else {
                    showAlertDialog(FindPwdActivity.this, getString(R.string.prompt), resp.getErrMessage());
                }
            } else if (action.equals(Do_ModifyResetPassword)) {
                if (resp.getResponseCode().equals("0")) {
                    this.finish();
                    Toast.makeText(this, R.string.findpassword_success, Toast.LENGTH_SHORT).show();
                    return true;
                } else {
                    showAlertDialog(FindPwdActivity.this, getString(R.string.prompt), resp.getErrMessage());
                    return false;
                }
            }
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean netResponse(String action, Hashtable<String, Object> resp,
                               boolean showAlert) {
        boolean result = super.netResponse(action, resp, showAlert);

        if (action.equals(Do_SendPhoneCode)) {
            if (result) {
                sendVerifycodeButton.cancelLocker();
                return true;
            }

        } else if (action.equals(Do_ModifyResetPassword)) {
            // 如果出现错误，直接返回
            if (result) return true;
            int responseCode;
            try {
                byte data[] = (byte[]) resp.get(HttpEngine.Key_RespData);
                LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, isDataCompile);
                responseCode = CommonUtils.objectToInt(luaTable.rawget("responseCode"));
                if (responseCode == 0) {
                    this.finish();
                    Toast.makeText(this, R.string.findpassword_success, Toast.LENGTH_SHORT).show();
                    return true;
                } else {
                    String errMessage = CommonUtils.objectToString(luaTable.rawget("errMessage"));
                    showAlertDialog(FindPwdActivity.this, getString(R.string.prompt), errMessage);
                    return false;
                }
            } catch (Exception e) {
                HPLog.e("FindPwdActivity", "", e);
                return true;
            }
        }
        return false;
    }
}
